package com.poh.controller;

import com.poh.pojo.Course;
import com.poh.pojo.User;
import com.poh.service.impl.CourseServiceImpl;
import com.poh.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;


@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    CourseServiceImpl courseService;

    @GetMapping("/" )
    public String toIndex() {
        return "login";
    }

    @GetMapping("/register")
    public String toRegister() {
        return "/register";
    }

    @PostMapping("/register")
    public String register(User user, String qPassword, Model model) {
        if (user.getPassWord().equals(qPassword)) {
            userService.addUser(user);
            return "redirect:/login";
        } else {
            model.addAttribute("msg", "两次密码不一致");
            return "/register";
        }
    }

    @PostMapping("/login")
    public String userLogin(@ModelAttribute("userName") String userName,
                            @ModelAttribute("passWord") String passWord,
                            Model model, HttpSession session) {
        User user = userService.userLogin(userName, passWord);

        System.out.println(user);
        if (user == null) {
            model.addAttribute("msg", "密码错误");
            return "login";
        }else {
            session.setAttribute("user", user);
            return "redirect:/getCourse";
        }
    }


    @GetMapping("/editUser")
    public String editUser() {
        return "user/editUser";
    }
    @PostMapping("/updateUser")
    public String updateUser(User user, HttpSession session) {
        userService.updateUserById(user);
        User oldUser = (User) session.getAttribute("user");
        user.setUserImg(oldUser.getUserImg());
        session.setAttribute("user", user);
        return "redirect:/editUser";
    }

    @PostMapping("/upload")
    public String uploadHeaderImg(@RequestPart("headerImg")MultipartFile headerImg,
                                  HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        if (!headerImg.isEmpty()) {
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("F:\\JavaWeb\\Course_SpringBoot\\src\\main\\resources\\static\\img\\" + originalFilename));
            userService.updateImgByUserId(user.getUserId(), originalFilename);
            user.setUserImg(originalFilename);
//            System.out.println("123123" + user.getUserImg());
            session.setAttribute("user", user);
            System.out.println(user.getUserImg());
            return "redirect:editUser";
        }
        return "redirect:editUser";
    }

    @PostMapping("/addCourse")
    public String addCourse(String invitationCode,
                            HttpSession session) {
        User user = (User) session.getAttribute("user");
        int courseIdByInvitationCode = userService.getCourseIdByInvitationCode(invitationCode);
        userService.studentAddCourse(user.getUserId(), courseIdByInvitationCode);
        return "redirect:/getCourse";
    }


    @GetMapping("/toStudentCourse/{courseId}")
    public String toStudentCourse(@PathVariable("courseId") int courseId,
                                  HttpSession session) {
        Course course = courseService.getCourseByCourseId(courseId);
        session.setAttribute("course", course);
        return "course/studentCourse";
    }

    @GetMapping("/exitUser")
    public String exitUser(HttpSession session) {
        session.removeAttribute("user");
        session.removeAttribute("course");
        return "/login";
    }

}
